package SmallClass

import (
	session2 "OnlineRetailers/controllers/admin/session"
	"OnlineRetailers/controllers/errorDeal"
	error2 "OnlineRetailers/models/errorDeal"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
)

type DeleteSmallClass struct {
	beego.Controller
}

func (c *DeleteSmallClass) DeleteSmallClass() {

	defer func() {
		c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
		c.Ctx.Output.Status = 200
		c.ServeJSON()
	}()
	var addr = "/SmallClass/DeleteSmallClass"
	//res := resMes.GetAdminUser{}
	//user := []*Admin.User{}
	log := &error2.Log{}
	mes := &error2.Mes{}
	var err error
	o := orm.NewOrm()

	//sessionUnkown := c.Ctx.Request.Header["Session"]
	err = session2.CheckSession(c.Ctx.Request.Header["Session"], 0)
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}

	id := c.GetString("id")

	err = DeleteClassM2M(o, id)
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}
	//fmt.Println(key)

	num, err := o.QueryTable("SmallClass").Filter("SmallClassId__exact", id).Delete()
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	} else if num == 0 {
		c.Data["json"] = errorDeal.ErrLightMess(mes, "400", "失败")
		return
	}

	mes.Mes = "success"
	mes.Code = "200"
	c.Data["json"] = mes
}

func DeleteClassM2M(o orm.Ormer, id string) (err error) {

	sql := "delete from `small_class_classifications` where `small_class_id`=?"
	_, err = o.Raw(sql, id).Exec()
	if err != nil {

		return
	}
	return nil
}
